package com.ccc.boot.system.controller;

import com.ccc.boot.exception.BusinessException;
import com.ccc.boot.response.ResponseResult;
import com.ccc.boot.system.pojo.FileParam;
import com.ccc.boot.system.service.SysFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 文件操作 REST接口
 *
 * @author superC
 * @version 0.1.0
 * @date 2022/4/7
 */
@RestController
@RequestMapping("/file")
public class FileController {

    protected static final Logger logger = LoggerFactory.getLogger(FileController.class);

    @Autowired
    private SysFileService fileService;

    /**
     * 上传文件至磁盘存储
     *
     * @param file
     * @return {@link ResponseResult}
     * @date 2022/4/7 9:06
     * @author superC
     */
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseResult upload(@RequestParam MultipartFile file) {
        try {
            Long fileId = fileService.upload(file);
            return ResponseResult.success("上传文件成功", fileId);
        } catch (Exception e) {
            logger.error("【捕获异常-上传文件操作】\r\n异常记录：", e);
            throw new BusinessException(e.getMessage());
        }
    }

    /**
     * 上传图片至数据库存储
     *
     * @param file
     * @return {@link ResponseResult}
     * @date 2022/4/7 9:06
     * @author superC
     */
    @PostMapping(value = "/uploadImage", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseResult uploadImage(@RequestParam MultipartFile file) {
        try {
            Long fileId = fileService.upload2DB(file);
            return ResponseResult.success("上传文件成功", fileId);
        } catch (Exception e) {
            logger.error("【捕获异常-上传文件操作】\r\n异常记录：", e);
            throw new BusinessException(e.getMessage());
        }
    }

    /**
     * 删除图片
     *
     * @date 2022/4/7 15:46
     * @param fileParam
     * @return {@link ResponseResult}
     * @author superC
     */
    @PostMapping("/deleteImage")
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteImage(@RequestBody FileParam fileParam) {
        try {
            boolean result = fileService.deleteFile(fileParam);
            return ResponseResult.success("删除文件成功", result);
        } catch (Exception e) {
            logger.error("【捕获异常-删除文件操作】\r\n异常记录：", e);
            //手工回滚异常
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new BusinessException(e.getMessage());
        }
    }

    /**
     * 下载文件
     *
     * @date 2022/4/7 16:42
     * @param fileId
     * @param response
     * @return {@link ResponseResult}
     * @author superC
     */
    @GetMapping("/downloadFile/{fileId}")
    public ResponseResult downloadFile(@PathVariable("fileId") Long fileId, HttpServletResponse response) {
        try {
            fileService.getFile(fileId, response);
            return ResponseResult.success();
        } catch (Exception e) {
            logger.error("【捕获异常-获取文件操作】\r\n异常记录：", e);
            throw new BusinessException(e.getMessage());
        }
    }

    /**
     * 获取图片的base64编码
     *
     * @date 2022/4/7 16:35
     * @param fileId
     * @return {@link ResponseResult}
     * @author superC
     */
    @GetMapping("/getImage/{fileId}")
    public ResponseResult getImage(@PathVariable("fileId") Long fileId) {
        try {
            return ResponseResult.success("获取文件成功", fileService.getImage(fileId));
        } catch (Exception e) {
            logger.error("【捕获异常-获取文件操作】\r\n异常记录：", e);
            throw new BusinessException(e.getMessage());
        }
    }

}